import 'package:easy_localization/easy_localization.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import 'package:workmate/models/api.dart';
import 'package:workmate/pages/splash.dart';
import 'package:workmate/provider/locale.dart';
import 'package:workmate/provider/theme.dart';
import 'package:workmate/routers/index.dart';
import 'package:workmate/provider/global.dart';
import 'package:workmate/utils/http.dart';

class Application extends StatefulWidget {
  const Application({Key? key}) : super(key: key);

  @override
  State<Application> createState() => _ApplicationState();
}

class _ApplicationState extends State<Application> {
  bool isBlack = false;
  @override
  void initState() {
    super.initState();
    globalInit();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint(context.locale.toString());

    return MultiProvider(
        providers: APP.getProviders(),
        child: Consumer2<ThemeProvider, LocaleProvider>(
            builder: (context, themeProvider, localeProvider, child) {
          return ColorFiltered(
              colorFilter: ColorFilter.mode(
                const Color.fromARGB(255, 255, 255, 255), // 主题滤镜
                themeProvider.blendMode,
                // BlendMode.color,
              ),
              child: Stack(
                  alignment: Alignment.center,
                  fit: StackFit.expand,
                  children: [
                    buildGestureDetector(MaterialApp(
                        localizationsDelegates: context.localizationDelegates,
                        supportedLocales: context.supportedLocales,
                        locale: context.locale,
                        navigatorKey: APP.navigatorKey,
                        title: 'Flutter demo',
                        debugShowCheckedModeBanner: false,
                        onGenerateRoute: APP.router.generator,
                        navigatorObservers: [APP.routeObserver],
                        home: const SplashPage(),
                        theme: themeProvider.themeData(),
                        builder: EasyLoading.init())),
                    // 亮度调节
                    IgnorePointer(
                        child: Container(
                            decoration: BoxDecoration(
                                color: Color.fromARGB(
                                    themeProvider.lightNumber, 0, 0, 0))))
                  ]));
        }));
  }

  // 隐藏键盘
  GestureDetector buildGestureDetector(Widget widget) {
    return GestureDetector(
        onTap: () {
          SystemChannels.textInput.invokeMethod('TextInput.hide');
        },
        child: widget);
  }

  void globalInit() async {
    // 初始化相关全局信息
    APP.navigatorKey = GlobalKey<NavigatorState>();
    APP.event = EventBus();
    APP.router = Routers();
    APP.http = HttpManager();
    APP.api = ApiModel.fromJson({});
    await Future.delayed(const Duration(milliseconds: 3000));
    debugPrint('initState Application');
  }

}
